﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Hotel_application.Models;
using System.IO;
namespace Hotel_application.Controllers
{
    public class ManagerController : Controller
    {
        //
        // GET: /Manager/

        public ActionResult ShowRoomType()
        {
            var ks = new KhachSanEntities();
            return View(ks);
        }

        public ActionResult ShowRoomClass()
        {
            var ks = new KhachSanEntities();
            return View(ks);
        }

        

        public ActionResult ShowPrice()
        {
            var ks = new KhachSanEntities();
            return View(ks);
        }

        public ActionResult ShowFloor()
        {
            var ks = new KhachSanEntities();
            return View(ks);
        }


        public ActionResult ShowRoom()
        {
            var ks = new KhachSanEntities();
            return View(ks);
        }


        [HttpGet]
        public ActionResult Search_Room()
        {
            var ks = new KhachSanEntities();
            var maTang = Request.QueryString["maTang"];
            var maDay = Request.QueryString["maDay"];
            var res = ks.proc_LayThongTinPhong(maDay, maTang);
            return PartialView(res);
        }


        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase fileData)
        {
            string file = Path.GetFileName(fileData.FileName);
            var fileName = Server.MapPath("~/Images/" + Path.GetFileName(fileData.FileName));
            if (fileData != null && fileData.ContentLength > 0 && !System.IO.File.Exists(fileName))
            {
                fileData.SaveAs(fileName);
                //string delete_path = Server.MapPath("/Images/calendar.gif");
                //System.IO.File.Delete(delete_path);
                return Json(true);
            }
            return Json(false);
        }

        [HttpGet]
        public ActionResult Update_RoomType()
        {
            //try
            //{
                var ks = new KhachSanEntities();
                string id = Request.QueryString["maLoai"].Substring(1,6);
                LoaiPhong lp = ks.LoaiPhongs.First(i => i.maLoai == id);
                lp.moTa = Request.QueryString["moTa"];
                lp.tenLoai = Request.QueryString["tenLoai"];
                lp.soLuong = Convert.ToInt16(Request.QueryString["soLuong"]);
                //lp.img_name = Request.QueryString["img_name" + id];
                ks.SaveChanges();
                return Content("Cập nhật thành công");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}
            
        }


        [HttpGet]
        public ActionResult Add_RoomType()
        {
            //try
            //{
            //lay ma loai
            var ks = new KhachSanEntities();
            string max_maLoai = ks.LoaiPhongs.Select(e => e.maLoai).OrderByDescending(p => p).FirstOrDefault();
            max_maLoai = max_maLoai.Substring(4);
            int max_maLoai_int = Convert.ToInt32(max_maLoai);
            max_maLoai_int += 1;
            max_maLoai = Convert.ToString(max_maLoai_int).PadLeft(2, '0');
            string maLoai = "LOAI" + max_maLoai;


            LoaiPhong lp = new LoaiPhong();
            lp.maLoai = maLoai;
            lp.moTa = Request.QueryString["moTa"];
            lp.tenLoai = Request.QueryString["tenLoai"];
            lp.soLuong = Convert.ToInt16(Request.QueryString["soLuong"]);
            ks.LoaiPhongs.Add(lp);
            //lp.img_name = Request.QueryString["img_name" + id];
            ks.SaveChanges();
            return Content("Thêm loại phòng mới thành công");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }


        [HttpGet]
        public ActionResult Delete_RoomType()
        {
            //try
            //{
            var ks = new KhachSanEntities();
            string id = Request.QueryString["maLoai"];
            LoaiPhong lp = ks.LoaiPhongs.First(i => i.maLoai == id);
            ks.LoaiPhongs.Remove(lp);
            ks.SaveChanges();
            return Content("Đã xóa loại phòng");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }




        [HttpGet]
        public ActionResult Update_RoomClass()
        {
            //try
            //{
            var ks = new KhachSanEntities();
            string id = Request.QueryString["maHang"].Substring(1, 6);
            HangPhong lp = ks.HangPhongs.First(i => i.maHang == id);
            lp.moTa = Request.QueryString["moTa"];
            lp.tenHang = Request.QueryString["tenHang"];
            //lp.img_name = Request.QueryString["img_name" + id];
            ks.SaveChanges();
            return Content("Cập nhật thành công");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }


        [HttpGet]
        public ActionResult Add_RoomClass()
        {
            //try
            //{
            //lay ma loai
            var ks = new KhachSanEntities();
            string max_maHang = ks.HangPhongs.Select(e => e.maHang).OrderByDescending(p => p).FirstOrDefault();
            max_maHang = max_maHang.Substring(4);
            int max_maHang_int = Convert.ToInt32(max_maHang);
            max_maHang_int += 1;
            max_maHang = Convert.ToString(max_maHang_int).PadLeft(2, '0');
            string maHang = "Hang" + max_maHang;


            HangPhong hp = new HangPhong();
            hp.maHang = maHang;
            hp.moTa = Request.QueryString["moTa"];
            hp.tenHang = Request.QueryString["tenHang"];
            ks.HangPhongs.Add(hp);
            //lp.img_name = Request.QueryString["img_name" + id];
            ks.SaveChanges();
            return Content("Thêm hạng phòng mới thành công");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }


        [HttpGet]
        public ActionResult Delete_RoomClass()
        {
            //try
            //{
            var ks = new KhachSanEntities();
            string id = Request.QueryString["maHang"];
            HangPhong hp = ks.HangPhongs.First(i => i.maHang == id);
            ks.HangPhongs.Remove(hp);
            ks.SaveChanges();
            return Content("Đã xóa hạng phòng");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }


        [HttpGet]
        public ActionResult Update_RoomPrice()
        {
            //try
            //{
            var ks = new KhachSanEntities();
            string maHang = Request.QueryString["maHang"];
            string maLoai = Request.QueryString["maLoai"];
            DonGia dg = ks.DonGias.First(i => i.maHang == maHang && i.maLoai == maLoai );
            dg.donGia1 = Convert.ToInt32(Request.QueryString["donGia"]);
            ks.SaveChanges();
            return Content("Cập nhật thành công");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }


        [HttpGet]
        public ActionResult Delete_RoomPrice()
        {
            //try
            //{
            var ks = new KhachSanEntities();
            string maHang = Request.QueryString["maHang"];
            string maLoai = Request.QueryString["maLoai"];
            DonGia dg = ks.DonGias.First(i => i.maHang == maHang && i.maLoai == maLoai);
            ks.DonGias.Remove(dg);
            ks.SaveChanges();
            return Content("Đã xóa đơn giá này");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }


        [HttpGet]
        public ActionResult Add_RoomPrice()
        {
            //try
            //{
            //lay ma loai
            var ks = new KhachSanEntities();
            DonGia dg = new DonGia();
            dg.maHang = Request.QueryString["maHang"];
            dg.maLoai = Request.QueryString["maLoai"];
            dg.donGia1 = Convert.ToInt32(Request.QueryString["donGia"]);
            ks.DonGias.Add(dg);
            //lp.img_name = Request.QueryString["img_name" + id];
            ks.SaveChanges();
            return Content("Thêm đơn giá mới thành công");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }



        [HttpGet]
        public ActionResult Update_RoomFloor()
        {
            //try
            //{
            var ks = new KhachSanEntities();
            string maTang = Request.QueryString["maTang"];
            string tenTang = Request.QueryString["tenTang"];
            Tang t = ks.Tangs.First(i => i.maTang == maTang);
            t.tenTang = Convert.ToInt16(tenTang);
            ks.SaveChanges();
            return Content("Cập nhật thành công");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }


        [HttpGet]
        public ActionResult Add_RoomFloor()
        {
            //try
            //{
            //lay ma loai
            var ks = new KhachSanEntities();
            string max_maTang = ks.Tangs.Select(e => e.maTang).OrderByDescending(p => p).FirstOrDefault();
            max_maTang = max_maTang.Substring(4);
            int max_maTang_int = Convert.ToInt32(max_maTang);
            max_maTang_int += 1;
            max_maTang = Convert.ToString(max_maTang_int).PadLeft(6, '0');
            string maTang = "TANG" + max_maTang;


            Tang t = new Tang();
            t.maTang = maTang;
            t.tenTang = Convert.ToInt16(Request.QueryString["tenTang"]);
            ks.Tangs.Add(t);
            //lp.img_name = Request.QueryString["img_name" + id];
            ks.SaveChanges();
            return Content("Thêm tầng mới thành công");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }



        [HttpGet]
        public ActionResult Delete_RoomFloor()
        {
            //try
            //{
            var ks = new KhachSanEntities();
            string maTang = Request.QueryString["maHang"];
            Tang dg = ks.Tangs.First(i => i.maTang == maTang);
            ks.Tangs.Remove(dg);
            ks.SaveChanges();
            return Content("Đã xóa tầng này");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }


        [HttpGet]
        public ActionResult Update_RoomBlock()
        {
            //try
            //{
            var ks = new KhachSanEntities();
            string maTang = Request.QueryString["maDayPhong"];
            string tenTang = Request.QueryString["tenDayPhong"];
            DayPhong t = ks.DayPhongs.First(i => i.maDayPhong == maTang);
            t.tenDayPhong = tenTang;
            ks.SaveChanges();
            return Content("Cập nhật thành công");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }


        public ActionResult Add_RoomBlock()
        {
            //try
            //{
            //lay ma loai
            var ks = new KhachSanEntities();
            string max_maDayPhong = ks.DayPhongs.Select(e => e.maDayPhong).OrderByDescending(p => p).FirstOrDefault();
            max_maDayPhong = max_maDayPhong.Substring(3);
            int max_maDayPhong_int = Convert.ToInt32(max_maDayPhong);
            max_maDayPhong_int += 1;
            max_maDayPhong = Convert.ToString(max_maDayPhong_int).PadLeft(7, '0');
            string maDay= "DAY" + max_maDayPhong;


            DayPhong t = new DayPhong();
            t.maDayPhong = maDay;
            t.tenDayPhong = Request.QueryString["tenDay"];
            ks.DayPhongs.Add(t);
            //lp.img_name = Request.QueryString["img_name" + id];
            ks.SaveChanges();
            return Content("Thêm dãy mới thành công");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }


        [HttpGet]
        public ActionResult Delete_RoomBlock()
        {
            //try
            //{
            var ks = new KhachSanEntities();
            string maTang = Request.QueryString["maHang"];
            DayPhong dg = ks.DayPhongs.First(i => i.maDayPhong == maTang);
            ks.DayPhongs.Remove(dg);
            ks.SaveChanges();
            return Content("Đã xóa tầng này");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }

        [HttpGet]
        public ActionResult Update_Room()
        {
            //try
            //{
            var ks = new KhachSanEntities();
            string maPhong = Request.QueryString["maPhong"];
            Phong p = ks.Phongs.First(i => i.maPhong == maPhong);
            p.thuocDay = Request.QueryString["maDay"];
            p.thuocTang = Request.QueryString["maTang"];
            p.maLoai = Request.QueryString["maLoai"];
            p.maHang = Request.QueryString["maHang"];
            ks.SaveChanges();
            return Content("Cập nhật thành công");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }

        [HttpGet]
        public ActionResult Add_Room()
        {
            //try
            //{
            //lay ma loai
            var ks = new KhachSanEntities();
            string max_maPhong = ks.Phongs.Select(e => e.maPhong).OrderByDescending(p => p).FirstOrDefault();
            max_maPhong = max_maPhong.Substring(5);
            int max_maPhong_int = Convert.ToInt32(max_maPhong);
            max_maPhong_int += 1;
            max_maPhong = Convert.ToString(max_maPhong_int).PadLeft(5, '0');
            string maPhong = "PHONG" + max_maPhong;


            Phong t = new Phong();
            t.maPhong = maPhong;
            t.maLoai = Request.QueryString["maLoai"];
            t.thuocTang = Request.QueryString["maTang"];
            t.thuocDay = Request.QueryString["maDay"];
            t.maHang = Request.QueryString["maHang"];
            ks.Phongs.Add(t);
            //lp.img_name = Request.QueryString["img_name" + id];
            ks.SaveChanges();
            return Content("Thêm phòng mới thành công");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }


        [HttpGet]
        public ActionResult Delete_Room()
        {
            //try
            //{
            var ks = new KhachSanEntities();
            string maPhong = Request.QueryString["maPhong"];
            Phong dg = ks.Phongs.First(i => i.maPhong == maPhong);
            ks.Phongs.Remove(dg);
            ks.SaveChanges();
            return Content("Đã xóa phòng này");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }





       
    }
}

